import { Injectable } from "@nestjs/common";
import { DeputyGeneralService } from "../services/deputy-general.service";

@Injectable()
export class DeputyWsHandlers {
  constructor(private readonly deputyGeneralService: DeputyGeneralService) {}

  /**
   * 查看副将详情 (1009)
   */
  async handleDeputyDetail(data: any, playerId: number) {
    try {
      const { id } = data;
      const result = await this.deputyGeneralService.getDeputyDetail(
        id,
        playerId
      );
      return {
        act_id: 1009,
        code: 0,
        data: result,
      };
    } catch (error: any) {
      return {
        act_id: 1009,
        code: error.code || 1001,
        msg: error.message || "副将不存在",
      };
    }
  }

  /**
   * 查看副将列表 (1119) - 最多10个副将，无需分页
   */
  async handleDeputyList(data: any, playerId: number) {
    try {
      const result = await this.deputyGeneralService.getDeputyList(playerId);
      return {
        act_id: 1119,
        code: 0,
        list: result.list,
      };
    } catch (error: any) {
      return {
        act_id: 1119,
        code: error.code || 1001,
        msg: error.message || "获取副将列表失败",
      };
    }
  }

  /**
   * 查询副将属性信息 (1110)
   */
  async handleDeputyAttributes(data: any, playerId: number) {
    try {
      const { cid: deputyId } = data || {};

      if (!deputyId) {
        return {
          act_id: 1110,
          code: -1,
          msg: "副将ID不能为空",
        };
      }

      const result = await this.deputyGeneralService.getDeputyAttributes(
        deputyId,
        playerId
      );
      return {
        act_id: 1110,
        code: 0,
        data: result,
      };
    } catch (error: any) {
      return {
        act_id: 1110,
        code: error.code || 1001,
        msg: error.message || "查询副将属性失败",
      };
    }
  }

  /**
   * 洗副将属性 (1122)
   */
  async handleWashDeputyAttribute(data: any, playerId: number) {
    try {
      const { cid: deputyId, py: attributeType } = data || {};

      if (!deputyId) {
        return {
          act_id: 1122,
          code: -1,
          msg: "副将ID不能为空",
        };
      }

      if (!attributeType) {
        return {
          act_id: 1122,
          code: -1,
          msg: "属性类型参数不能为空",
        };
      }

      const result = await this.deputyGeneralService.washDeputyAttribute(
        deputyId,
        playerId,
        attributeType
      );
      return {
        act_id: 1122,
        code: 0,
        data: result,
      };
    } catch (error: any) {
      return {
        act_id: 1122,
        code: error.code || 1001,
        msg: error.message || "洗副将属性失败",
      };
    }
  }

  /**
   * 副将使用技能 (1131)
   */
  async handleDeputyUseSkill(data: any, playerId: number) {
    try {
      const { id, skill_id, target_id } = data;
      const result = await this.deputyGeneralService.useSkill(
        id,
        skill_id,
        target_id,
        playerId
      );
      return {
        act_id: 1131,
        code: 0,
        data: result,
      };
    } catch (error: any) {
      return {
        act_id: 1131,
        code: error.code || 1001,
        msg: error.message || "技能使用失败",
      };
    }
  }

  /**
   * 副将技能升级 (1132)
   */
  async handleDeputySkillUpgrade(data: any, playerId: number) {
    try {
      const { id, skill_id, cost_items } = data;
      const result = await this.deputyGeneralService.upgradeSkill(
        id,
        skill_id,
        cost_items,
        playerId
      );
      return {
        act_id: 1132,
        code: 0,
        data: result,
      };
    } catch (error: any) {
      return {
        act_id: 1132,
        code: error.code || 1001,
        msg: error.message || "技能升级失败",
      };
    }
  }

  /**
   * 副将学习技能 (8714)
   */
  async handleDeputyLearnSkill(data: any, playerId: number) {
    try {
      const { id, skill_id } = data;
      const result = await this.deputyGeneralService.learnSkill(
        id,
        skill_id,
        playerId
      );
      return {
        act_id: 8714,
        code: 0,
        data: result,
      };
    } catch (error: any) {
      return {
        act_id: 8714,
        code: error.code || 1001,
        msg: error.message || "学习技能失败",
      };
    }
  }

  /**
   * 副将学习/重修技能 (1396)
   * action=1 学习（沿用 learnSkill）；action=2 重修
   */
  async handleDeputyLearnOrRelearn(data: any, playerId: number) {
    try {
      const { cid, action, skillid } = data || {};
      if (!cid || !action) {
        return { act_id: 1396, code: 8, msg: "参数不完整" };
      }

      if (Number(action) === 1) {
        // 支持 skillid 为空或0时随机学习
        const r = await this.deputyGeneralService.learnSkillDynamic(
          playerId,
          Number(cid),
          skillid ? Number(skillid) : undefined
        );
        return { act_id: 1396, code: 0, data: r.msg };
      }

      if (Number(action) === 2) {
        const r = await this.deputyGeneralService.relearnDeputySkill(
          playerId,
          Number(cid),
          skillid ? Number(skillid) : undefined
        );
        return { act_id: 1396, code: 0, data: r.msg };
      }

      return { act_id: 1396, code: 8, msg: "无效的操作类型" };
    } catch (error: any) {
      return {
        act_id: 1396,
        code: error.code || 8,
        msg: error.message || "处理失败",
      };
    }
  }

  /**
   * 副将升星
   */
  async handleDeputyStarUpgrade(data: any, playerId: number) {
    try {
      const { id, cost_items } = data;
      const result = await this.deputyGeneralService.upgradeStar(
        id,
        cost_items,
        playerId
      );
      return {
        act_id: 8701,
        code: 0,
        data: result,
      };
    } catch (error: any) {
      return {
        act_id: 8701,
        code: error.code || 1001,
        msg: error.message || "升星失败",
      };
    }
  }

  /**
   * 副将转职 (8712)
   */
  async handleDeputyTransformation(data: any, playerId: number) {
    try {
      const { id } = data;
      const result = await this.deputyGeneralService.transform(id, playerId);
      return {
        act_id: 8712,
        code: 0,
        data: result,
      };
    } catch (error: any) {
      return {
        act_id: 8712,
        code: error.code || 1001,
        msg: error.message || "转职失败",
      };
    }
  }

  /**
   * 副将解雇 (8711)
   */
  async handleDeputyDismiss(data: any, playerId: number) {
    try {
      const { id } = data;
      const result = await this.deputyGeneralService.dismiss(id, playerId);
      return {
        act_id: 8711,
        code: 0,
        data: result,
      };
    } catch (error: any) {
      return {
        act_id: 8711,
        code: error.code || 1001,
        msg: error.message || "解雇失败",
      };
    }
  }

  /**
   * 副将重新培养 (8703)
   */
  async handleDeputyRetrain(data: any, playerId: number) {
    try {
      const { id } = data;
      const result = await this.deputyGeneralService.retrain(id, playerId);
      return {
        act_id: 8703,
        code: 0,
        data: result,
      };
    } catch (error: any) {
      return {
        act_id: 8703,
        code: error.code || 1001,
        msg: error.message || "重新培养失败",
      };
    }
  }

  /**
   * 副将默契培养 (8718)
   */
  async handleDeputyMqdCultivation(data: any, playerId: number) {
    try {
      const { id } = data;
      const result = await this.deputyGeneralService.cultivateMqd(id, playerId);
      return {
        act_id: 8718,
        code: 0,
        data: result,
      };
    } catch (error: any) {
      return {
        act_id: 8718,
        code: error.code || 1001,
        msg: error.message || "默契培养失败",
      };
    }
  }

  /**
   * 副将生成 (8765)
   */
  async handleDeputyGeneration(data: any, playerId: number) {
    try {
      const { base_id, player_id } = data;
      const result = await this.deputyGeneralService.generateDeputy(
        base_id,
        player_id
      );
      return {
        act_id: 8765,
        code: 0,
        data: result,
      };
    } catch (error: any) {
      return {
        act_id: 8765,
        code: error.code || 1001,
        msg: error.message || "副将生成失败",
      };
    }
  }

  /**
   * 副将上阵/下阵
   */
  async handleDeputyDeploy(data: any, playerId: number) {
    try {
      const { id, is_deployed } = data;
      const result = await this.deputyGeneralService.deployDeputy(
        id,
        is_deployed,
        playerId
      );
      return {
        act_id: 8702,
        code: 0,
        data: result,
      };
    } catch (error: any) {
      return {
        act_id: 8702,
        code: error.code || 1001,
        msg: error.message || "上阵操作失败",
      };
    }
  }

  /**
   * 查看副将列表（1419）- 包括可以放到仓库的副将和仓库中的副将
   */
  async handleDeputyWarehouseList(data: any, playerId: number) {
    try {
      const { action, pageNum, sort } = data;
      const result = await this.deputyGeneralService.getDeputyWarehouseList(
        +action,
        pageNum,
        sort,
        playerId
      );
      return {
        act_id: 1419,
        code: 0,
        list: result.list,
        sum: result.sum,
        type: result.type,
      };
    } catch (error: any) {
      return {
        act_id: 1419,
        code: error.code || 1001,
        msg: error.message || "获取副将列表失败",
        list: [],
        sum: 0,
        type: 0,
      };
    }
  }

  /**
   * 副将仓库操作（1420）- 存储到仓库和从仓库提取
   */
  async handleDeputyWarehouseOperation(data: any, playerId: number) {
    try {
      const { id, action, pageNum } = data;
      const result = await this.deputyGeneralService.deputyWarehouseOperation(
        id,
        action,
        pageNum,
        playerId
      );
      return {
        act_id: 1420,
        code: 0,
        msg: result.msg,
      };
    } catch (error: any) {
      return {
        act_id: 1420,
        code: error.code || 1001,
        msg: error.message || "仓库操作失败",
      };
    }
  }

  /**
   * 副将上阵/下阵/位置交换操作 (2072)
   * act: 2-下阵, 3-位置交换, 4-上阵
   */
  async handleDeputyBattleOperation(data: any, playerId: number) {
    try {
      const { id: deputyId, act } = data;

      if (!deputyId || !act) {
        return {
          act_id: 2072,
          code: -1,
          msg: "缺少必要参数",
        };
      }

      let result;
      switch (act) {
        case 3: // 下阵
          result = await this.deputyGeneralService.removeDeputyFromBattle(
            deputyId,
            playerId
          );
          break;
        case 1: // 位置交换
          result = await this.deputyGeneralService.swapDeputyPosition(
            deputyId,
            playerId
          );
          break;
        case 4: // 上阵
          result = await this.deputyGeneralService.deployDeputyToBattle(
            deputyId,
            playerId
          );
          break;
        default:
          return {
            act_id: 2072,
            code: -1,
            msg: "无效的操作类型",
          };
      }

      return {
        act_id: 2072,
        code: 0,
        data: result,
      };
    } catch (error: any) {
      return {
        act_id: 2072,
        code: error.code || 1001,
        msg: error.message || "副将操作失败",
      };
    }
  }

  /**
   * 获取副将等级榜排名 (1194)
   */
  async handleDeputyLevelRanking(data: any, playerId: number) {
    try {
      const { pageNum = 0 } = data;

      // 验证参数
      if (typeof pageNum !== "number" || pageNum < 0) {
        return {
          act_id: 1194,
          code: -1,
          msg: "页码参数无效",
          list: [],
          page: 1,
          sum: 0,
        };
      }

      // 获取副将等级榜
      const result = await this.deputyGeneralService.getDeputyLevelRanking(
        pageNum,
        40 // 每页40条
      );

      return {
        act_id: 1194,
        code: 0,
        list: result.list,
        page: result.page,
        sum: result.sum,
      };
    } catch (error: any) {
      console.error("获取副将等级榜失败:", error);
      return {
        act_id: 1194,
        code: -1,
        msg: error.message || "获取副将等级榜失败",
        list: [],
        page: 1,
        sum: 0,
      };
    }
  }
}
